//
//  EventListParser.m
//  FootPrint
//
//  Created by alex tao on 3/26/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "EventListParser.h"
#import "EventQueryRequest.pb.h"
#import "EventManager.h"

@implementation EventListParser

- (void)dealloc
{
    _delegate = nil;
    [super dealloc];
}

- (void) quaryEventByUser:(long long)uid beginDate:(long long)bgDate endDate:(long long)edDate andFilter:(NSString*)filter
{
    if (uid <= 0) {
        return;
    }
    
    if (filter.length != 4) {
        filter = @"0000";
    }
    
    EventQueryRequestMessage_Builder * builder = [[EventQueryRequestMessage builder] setUserId:uid];
    
    if(bgDate > 0){
        [builder setBeginDate:bgDate];
    }
    if(edDate > 0){
        [builder setEndDate:edDate];
    }

    [builder setFilter:filter];
    
    NSData * queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_EVENT_QUARY postData:queryData completion:^(NSData * resultData) {
            @try {
                EventQueryResponseMessage * resp = [EventQueryResponseMessage parseFromData:resultData];
                [_delegate didRecieveEventList:resp beginDate:bgDate endDate:edDate withFilter:filter];
                [[EventManager sharedInstance] mergeList:resp.eventSumList beginDate:bgDate endDate:edDate];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"暂时无法获得事件信息!"] setDuration:2000] show];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
        }];
    }
}

- (void) createEventByUser:(long long)uid eventMsg:(EventMessage*)msg
{
    
}

@end
